package com.d4a5.mapping.google2
{
	import com.d4a5.mapping.core.MathStringParser;
	
	import flash.events.Event;

	public class wmgOverlayLayerEquationRenderer extends wmgOverlayLayerGradientRenderer
	{
		protected var _calculationOutputField:String;
		protected var _equation:String;
		
		
		public function wmgOverlayLayerEquationRenderer()
		{
			super();
			this._attribute = this._calculationOutputField = "equationRendererOutput";
		}
		
		
		public function get equation() : String {
			return this._equation;
		}
		public function set equation( eq:String ) : void {
			if( eq != this._equation ){
				this._equation = eq.replace( /(\t|\n|\r| )+/g , "" );
				
				this._needsUpdating = "equation";
				
				this.dispatchEvent( new Event( "renderer_changed" ) );
			}
		}
		
		override public function set attribute(a:String):void {
			
		}
		
		
		override public function startUpdating() : void {
			if( this._needsUpdating == "equation" ){
				this._minCalcValue = Number.POSITIVE_INFINITY;
				this._maxCalcValue = Number.NEGATIVE_INFINITY;
				this._sumCalcValue = 0;
				this._countCalcValue = 0;
				this._avgCalcValue = 0;
			}
			else{
				super.startUpdating();
			}
		}
		override public function update( overlay:wmgOverlayLayerBaseShape ) : void {
			if( this._needsUpdating == "equation" ){
				var value:Number = overlay[this._calculationOutputField]= MathStringParser.parse( this._equation , overlay.attributes );
				
				this._sumCalcValue += value;
				this._countCalcValue++;
				this._minCalcValue = ( value < this._minCalcValue ) ? value : this._minCalcValue;
				this._maxCalcValue = ( value > this._maxCalcValue ) ? value : this._maxCalcValue;
			}
			else{
				super.update( overlay );
			}
		}
		override public function endUpdating() : void {
			if( this._needsUpdating == "equation" ){
				this._needsUpdating = null;
				this._avgCalcValue = this._sumCalcValue / this._countCalcValue;
			}
			else{
				super.endUpdating();
			}
		}
		
		
	}
}